
let ipt_account = document.getElementById("ipt_account");
let ipt_pwd = document.getElementById("ipt_pwd");
let ipt_pwd2 = document.getElementById("ipt_pwd2");

function save(){
	let account = ipt_account.value;
	let pwd = ipt_pwd.value;
	let pwd2 = ipt_pwd2.value;
	//验证表单
	if(account.trim().length === 0){
		alert("请输入账号");
		return;
	}
	
	//密码不能为空
	if(pwd.trim().length < 6){
		alert("密码至少6位");
		return;
	}
	
	//确认密码，两次密码必须一致
	if(pwd !== pwd2){
		alert("两次密码必须一致");
		return;
	}
	
	/** 
	 * 验证账号是否重复 
	 * 	1、从localStorage中去获取用户注册用的数组
	 * 	2、遍历数组进行账号对比
	 * */
	 
	let users = JSON.parse(localStorage.getItem("users"));
	console.log(users);
	if(!users){
		users = new Array();
	}
	
	for(let index in users){
		let item = users[index];
		if(item.account === account){
			alert("该账号已被注册过");
			return;
		}
	}
	
	console.log(typeof(users));
	users.push({account,pwd}); //ES6支持的简写方式，key和vakue都同名
	localStorage.setItem("users",JSON.stringify(users));//保存到localStorage中
	alert("注册成功");
	location.href = "登录.html";
}